package cn.jiyun.controller;


import cn.jiyun.common.config.Result;
import cn.jiyun.entity.GoodsClassification;
import cn.jiyun.entity.GoodsList;
import cn.jiyun.service.IGoodsClassificationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/xm-goods/goods-classification")
public class GoodsClassificationController {
    @Autowired
    private IGoodsClassificationService goodsClassificationService;
    //    商品分类查询
    @RequestMapping("/findpage")
    public Result findtradepage(Integer current, Integer pageSize, @RequestBody GoodsClassification goodsClassification){
        LambdaQueryWrapper<GoodsClassification> qw = new LambdaQueryWrapper<>();
        if (goodsClassification.getId()!=null){
            qw.eq(GoodsClassification::getId,goodsClassification.getId());
        }
        if (goodsClassification.getClassification()!=null&&goodsClassification.getClassification().length()>0){
            qw.like(GoodsClassification::getClassification,goodsClassification.getClassification());
        }
        if (goodsClassification.getZtime() != null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if (goodsClassification.getZtime()==1) {
                qw.apply("ABS(DATEDIFF({0}, createtime)) <= {1}", now, mo);
            }
            if(goodsClassification.getZtime()==2){
                qw.apply("YEAR(createtime)=YEAR(CURRENT_DATE)");
            }
            if(goodsClassification.getZtime()==3){
                qw.apply("DATE_FORMAT(createtime,'%Y')=2022");
            }
            if(goodsClassification.getZtime()==4){
                qw.apply("DATE_FORMAT(createtime,'%Y')=2021");
            }
            if(goodsClassification.getZtime()==5){
                qw.apply("DATE_FORMAT(createtime,'%Y')=2020");
            }
            if(goodsClassification.getZtime()==6){
                qw.apply("DATE_FORMAT(createtime,'%Y')<2020");
            }
        }
        Page<GoodsClassification> page = goodsClassificationService.page(new Page<GoodsClassification>(current,pageSize), qw);
        return Result.ok(page);
    }
}
